#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include "cJSON.h"

#define N 10240
void *new_sock(void *arg)
{
    printf("will recv new connect\n");
    int sockfd, qtfilefd;
    char buf[10240];
    int addrlen = sizeof(struct sockaddr);
    struct sockaddr_in addr, clientaddr;
    int nbytes;

    // 创建一个套接字
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket err");
        pthread_exit(NULL); // 退出当前线程
    }

    // 绑定套接字
    bzero(&addr, addrlen);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("192.168.50.50");
    addr.sin_port = htons(5567); // 新的连接用8001端口

    if (bind(sockfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        pthread_exit(NULL);
    }

    // 启动监听
    if (listen(sockfd, 5) < 0)
    {
        perror("listen err");
        pthread_exit(NULL);
    }

    // 接收连接
    qtfilefd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen);
    if (qtfilefd < 0)
    {
        perror("accept err");
        pthread_exit(NULL);
    }

    printf("recv qt client\n");

    // 收发数据
    bzero(buf, 10240);
    int recv_n=0;
    recv_n =recv(qtfilefd, buf, 10240, 0);
    if(recv_n>0)
    {
        printf("copy....\n");
        // 拷贝配置文件
        int fd = open("/home/hq/lianxi/大项目设备采集/node.json", O_WRONLY | O_CREAT | O_TRUNC, 0777);
        if (fd < 0)
        {
            perror("open err");
            pthread_exit(NULL); // 退出线程
        }
        
        write(fd, buf,recv_n);
        close(fd);
        printf("copy success!\n");
    }
    else
    {
        perror("copy err");
    }

    pthread_exit(NULL); // 线程结束
}

int main(int argc, char const *argv[])
{
    int broadfd;
    
    // 创建 socket
    broadfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (broadfd < 0)
    {
        perror("sock err");
        return -1;
    }
    
    // 绑定套接字
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(7655); // 端口号
    addr.sin_addr.s_addr = INADDR_ANY;

    int addrlen = sizeof(addr);
    
    if (bind(broadfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }
    
    char buf[N] = {0};
    struct sockaddr_in cliaddr;
    
    bzero(buf, N);
    ssize_t len = recvfrom(broadfd, buf, N, 0, (struct sockaddr *)&cliaddr, &addrlen);
    
    // 判断是否收到正确的协议
    if (strcmp(buf, "coffee") != 0)
    {
        printf("not my coffee\n");
        return -1;
    }
    
    // 回复 yes
    sendto(broadfd, "yes", 4, 0, (struct sockaddr *)&cliaddr, addrlen);
    
    // 变身为 TCP 服务器
    int tcpfd = socket(AF_INET, SOCK_STREAM, 0);
    if (tcpfd < 0)
    {
        perror("sock err");
        return -1;
    }
    
    if (bind(tcpfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }
    
    // 监听套接字
    if (listen(tcpfd, 5) < 0)
    {
        perror("listen err");
        return -1;
    }
    
    printf("wait qt connect\n");
    int clifd = accept(tcpfd, NULL, NULL);
    if (clifd < 0)
    {
        perror("accept err");
        return -1;
    }
    
    printf("qt connect coming\n");
    
    send(clifd, "yes", 3, 0);
    
    while (1)
    {
        bzero(buf, N);
        int len = recv(clifd, buf, N, 0);
        if (len <= 0)
        {
            break;
        }
        
        cJSON *root = cJSON_Parse(buf);
        if (root == NULL)
        {
            printf("JSON parse error\n");
            continue; // 跳过无效的数据
        }

        cJSON *item = cJSON_GetObjectItem(root, "type");
        if (item != NULL)
        {
            int type = item->valueint;
            if (type == 1)
            {
                printf("tcp 链接成功\n");
                pthread_t tid;
                if (pthread_create(&tid, NULL, new_sock, NULL) != 0)
                {
                    perror("pthread_create err");
                }
            }
            else if (type == 3)
            {
                if (send(clifd, "living", 7, 0) < 0)
                {
                    perror("send err\n");
                }
            }
        }

        cJSON_Delete(root); // 释放 JSON 对象
    }
    
    return 0;
}
